package com.rope.backEnd.controller;

import com.rope.backEnd.pojo.ChatRequest;
import com.rope.backEnd.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
//import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;

@RestController
//@CrossOrigin(origins = "*")
@RequiredArgsConstructor
public class ChatController {
    private final ChatService chatService;

    @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter chat(@RequestBody ChatRequest request) {
        SseEmitter emitter = new SseEmitter(60_000L);
        try {
            chatService.streamChatCompletion(request, emitter);
        } catch (IOException e) {
            emitter.completeWithError(e);
        }
        return emitter;
    }
}
